<template>
	<view class="view">
		<div style='position: sticky;border-bottom: 1rpx solid #F1F1F1;'>
			<v-tabs :scroll="false" @change="changeTab" v-model="current" :tabs='tabs' activeColor="#000000"
				:lineScale="0.2" padding="0rpx" height="80rpx" line-height="6rpx" lineColor='#32C59A'
				currentColor='#00000'> </v-tabs>
		</div>

		<swiper easing-function='linear' :current="current" :style="{height: '100%'}" @change="swiperChange">
			<swiper-item v-for="(item,index) in tabs" :key='index'>
				<scroll-view class="list" scroll-y style="height: 100%;" @scrolltolower='loadMore()'>
					<view class="item" v-for="(item,index) in list" :key="index">
						<view class="info">
							<view>{{item.type | formatType}}{{item.productName || ''}}</view>
							<view>{{item.createTime}}<text>{{item.orderStatus | formatStatus}}</text></view>
						</view>
						<view class="integral">
							<view :class="item.status == 1 ? 'green' : ''">
								{{item.status == 1 ? '+' : '-'}}{{item.amount}}积分
							</view>
							<view>剩余 {{item.balance}} 积分</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userInfo').id,
				current: 0,
				tabs: ["全部", "签到", "兑换", "悬赏", "打赏"],
				finished: false,
				pageNo: 1,
				list: []
			};
		},
		filters: {
			formatType(type) {
				switch (type) {
					case 0:
						return '签到';
					case 1:
						return '兑换 | ';
					case 2:
						return '悬赏';
					case 3:
						return '打赏';
					case 4:
						return '充值';
					default:
						return '';
				}
			},
			formatStatus(status) {
				switch (status) {
					case 0:
						return '待付款';
					case 1:
						return '待发货';
					case 2:
						return '待收货';
					case 3:
						return '已完成';
					case 4:
						return '取消订单';
					default:
						return '';
				}
			}
		},
		created() {
			this.init();
		},
		methods: {
			loadMore() {
				this.getList();
			},
			init() {
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.getList();
			},
			changeTab(e) {
				this.current = e;
			},
			swiperChange(e) {
				this.current = e.detail.current;
				this.init();
			},
			getList() {
				if (this.finished) {
					return;
				}
				//记录类型 0签到 1兑换 2悬赏 3打赏 4充值
				this.$api.http('/user/userIntegralRecord/list', 'GET', {
					pageNo: this.pageNo++,
					pageSize: 20,
					type: this.current == 0 ? '' : this.current - 1,
					userId: this.userId
				}, res => {
					if (res.data.code == 200) {
						if (res.data.result.records.length < 20) {
							this.finished = true;
						}
						this.list = this.list.concat(res.data.result.records);
					} else {
						this.pageNo--;
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
					uni.stopPullDownRefresh();
				});
			},
			to(url) {
				uni.navigateTo({
					url
				});
			},
		},
		onPullDownRefresh() {
			this.init();
		},
	};
</script>

<style lang="less" scoped>
	.view {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.list {
			.item {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 1rpx;
				padding: 30rpx;
				background-color: #FFFFFF;

				.info {
					flex: 1;

					view {
						font-size: 24rpx;
						color: #999999;

						&:first-child {
							margin-bottom: 10rpx;
							color: #333333;
							font-size: 30rpx;
						}

						text {
							margin-left: 30rpx;
							color: #007AFF;
						}
					}
				}

				.integral {
					text-align: end;

					view {
						font-size: 24rpx;
						color: #999999;

						&:first-child {
							margin-bottom: 10rpx;
							font-size: 30rpx;
							color: #333333;
						}

						&.green {
							color: #39B54A;
						}
					}
				}
			}
		}
	}
</style>
